package br.sed.theenemy.presentation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import br.sed.theenemy.model.bean.User;

/**
 * 
 * @author JulioCB
 * @email jcbjulio@gmail.com
 * 
 */
public class UserSession {
	
	/** Key for retrieving Administrator user logged into session. */
    public static final String LOGGED_USER = "__LoggedUser";
    
    public static final String NONCE_URL = "__UserNonceUrl";
    public static final String NONCE = "__UserNonce";
    
    private final HttpSession session;
    
    /**
     * Constructor.
     * This constructor calls request.getSession(false) so a session is not created if it doesnŽt exists.
     * 
     * @param request The HTTP request we are processing
     */
    public UserSession(final HttpServletRequest request) {
        this(request, false);
    }
    
    /**
     * Constructor.
     * This constructor calls request.getSession(createSession).
     * 
     * @param request The HTTP request we are processing
     * @param createSession If the session must be create if it does not exists.
     */
    public UserSession(final HttpServletRequest request, final boolean createSession) {
        this.session = request.getSession(createSession);
    }
    
    /**
     * Constructor.
     * @param session The Session.
     */
    public UserSession(final HttpSession session) {
        this.session = session;
    }
    
    /**
     * Gets the logged on user session.
     * 
     * @return Object retrieved from session
     */
    public User getLoggedUser() {
        return (User) getAttribute(LOGGED_USER);
    }
    
    /**
     * Stores the object for the data user in the session.
     * 
     * @param adminUser
     *            Object to be stored in the session
     */
    public void setLoggedUser(final User user) {
        setAttribute(LOGGED_USER, user);
    }
    /**
     * Remove the object for the data user in the session.
     * 
     */
    public void removeLoggedUser() {
        removeAttribute(LOGGED_USER);
    }

    /**
     * Get nonce in the session.
     * @return Long nonce code
     */
    public Long getNonce() {
        final Long nonce = (Long) getAttribute(NONCE);
        setAttribute(NONCE, null);
        
        return nonce;
    }
    
    /**
     * Create nonce method.
     * @param actionUrl action url
     * @return creation time
     */
    public long createNonce(final String actionUrl) {
        final long currentTime = System.currentTimeMillis();
        setAttribute(NONCE_URL, actionUrl);
        setAttribute(NONCE, currentTime);
        
        return currentTime;
    }
    
    /**
     *  Get nonceUrl in the session.
     * @return String nonce url
     */
    public String getNonceUrl() {
        return (String) getAttribute(NONCE_URL);
    }
    
    /**
     * Gets the attribute on user session.
     * @param name The attribute name
     * @return Object retrieved from session
     */
    public Object getAttribute(final String name) {
        if (session == null) {
            return null;
        } else {
            return session.getAttribute(name);    
        }
    }

    /**
     * Set the attribute on user session.
     * 
     * @param name The attribute name
     * @param value The value to be set
     */
    public void setAttribute(final String name, final Object value) {
        if (session != null) {
            session.setAttribute(name, value);
        }
    }
    
    /**
     * Remove the attribute on user session.
     * 
     * @param name The attribute name
     */
    public void removeAttribute(final String name) {
        if (session != null) {
            session.removeAttribute(name);
        }        
    }
    
    /**
     * Invalidate the session.
     */
    public void invalidate() {
        if (session != null) {
            session.invalidate();
        }
    }
}
